package com.supertange.miniapp.wepet.common.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ConvertToMp4 {
    private String ffmpeg;

    public ConvertToMp4(String ffmpeg) {
        super();
        this.ffmpeg = ffmpeg;
    }

    public void convertor(String videoInputPath, String videoOutputPath) throws Exception {
        List<String> command = new ArrayList<>();
        command.add(ffmpeg);

        command.add("-i");
        command.add(videoInputPath);

        command.add("-y");
        command.add(videoOutputPath);

//		for (String c : command) {
//			System.out.print(c + " ");
//		}

        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = builder.start();

        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        }

        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }

    public static void main(String[] args) {
        ConvertToMp4 ffmpeg = new ConvertToMp4("ffmpeg");
        try {
            ffmpeg.convertor("/UsersEntity/hetang/Desktop/upload/video/20180926/743755cf137d4eaca8d10557e238c8a2.avi",  "/UsersEntity/hetang/Desktop/upload/video/20180926/743755cf137d4eaca8d10557e238c8a2.mp4");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
